package com.evon.yardmanagement.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.evon.yardmanagement.model.Oem;
import com.evon.yardmanagement.model.User;

public class UserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserDAO.class);

	public User findUser(String userName) {
		log.debug("finding  user instances having user name - " + userName);
		try {
			Criteria criteria = getSession().createCriteria(User.class);
			criteria.add(Restrictions.eq("userName", userName));
			return (User) criteria.uniqueResult();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public User getUser(long userId) {
		log.debug("finding  user instances having user id - " + userId);
		try {
			Criteria criteria = getSession().createCriteria(User.class);
			criteria.add(Restrictions.eq("id", userId));
			return (User) criteria.uniqueResult();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public Class getQueryClass() {
		// TODO Auto-generated method stub
		return User.class;
	}
	
	public List<User> geUsers(int companyId) {
		Criteria criteria = getSession().createCriteria(getQueryClass());
		criteria.createAlias("vendor", "vendor");
		criteria.add(Restrictions.eq("vendor.id", companyId));
		return criteria.list();
	}
	
	public List<User> getSpecificUsers(int userId) {
		Criteria criteria = getSession().createCriteria(getQueryClass());
		//criteria.createAlias("id", "id");
		criteria.add(Restrictions.eq("id", userId));
		System.out.println("....."+criteria.list());
		return criteria.list();
	}
}
